package com.dbl.oms.common.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回结果
 */
@ApiModel(value = "返回结果")
@Data
public class ResultData<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	// 处理成功
	public static final String OK = "0000";
	public static final String OK_MSG = "操作成功";
	// 其他错误
	public static final String ERR = "9999";
	// 其他错误
	public static final String ERR_MSG = "操作失败";

	@ApiModelProperty(value = "状态码")
	private String code = ERR;
	@ApiModelProperty(value = "状态描述")
	private String msg = "";
	@ApiModelProperty(value = "数据")
	private T data;

	/**
	 * 失败
	 *
	 * @return
	 */
	public static ResultData<?> getFailResult() {
		return new ResultData<>(ERR, ERR_MSG);
	}

	/**
	 * 失败
	 *
	 * @param message
	 * @return
	 */
	public static <T> ResultData<T> getFailResult(String message) {

		return new ResultData<T>(ERR, message);
	}

	/**
	 * 成功
	 *
	 * @param message
	 * @return
	 */
	public static <T> ResultData<T> getSuccessResult(String message) {
		return new ResultData<T>(OK, message);
	}

	/**
	 */
	public static <T> ResultData<T> getSuccessResult() {
		return new ResultData<T>(OK, OK_MSG);
	}

	/**
	 * 成功
	 *
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> ResultData<T> getSuccessData(T data) {
		return new ResultData<T>(OK, OK_MSG, data);
	}

	/**
	 * 成功
	 *
	 * @param data
	 * @param message
	 * @return
	 */
	public static <T> ResultData<T> getSuccessResult(T data, String message) {
		return new ResultData<T>(OK, message, data);
	}

	public ResultData() {
	}

	public ResultData(String code, String message) {
		this.code = code;
		// 根据code的前两位匹配获取对外提示的消息
		if (msg != null) {
			message = msg;
		}
		this.msg = message;
	}

	public ResultData(String code, String message, T result) {
		this.code = code;
		// 根据code的前两位匹配获取对外提示的消息
		if (msg != null) {
			message = msg;
		}
		this.msg = message;
		this.data = result;
	}

	public ResultData(T result) {
		this(OK, "操作成功！", result);
	}

	public T getData() {
		return data;
	}

	public ResultData<T> setData(T data) {
		this.data = data;
		return this;
	}

	public String getCode() {
		return code;
	}

}
